package com.manxi.mapper;

import com.github.pagehelper.Page;
import com.manxi.annotation.AutoFill;
import com.manxi.dto.TrendsDTO;
import com.manxi.dto.TrendsPageQueryDTO;
import com.manxi.entity.Trends;
import com.manxi.enumeration.OperationType;
import com.manxi.vo.TrendsVO;
import org.apache.ibatis.annotations.*;

@Mapper
public interface TrendsMapper {

    /**
     * 新增动态
     *
     * @param trends
     * @return
     */
    @Insert("insert into trends (title,image,content,create_time,update_time,create_user,update_user) values (#{title},#{image},#{content},#{createTime},#{updateTime},#{createUser},#{updateUser})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @AutoFill(value = OperationType.INSERT)
    void insert(Trends trends);

    /**
     * 动态分页查询
     *
     * @param trendsPageQueryDTO
     * @return
     */
    @Select("<script> " +
            "select * from trends " +
            "where 1 = 1 " +
            "<if test ='title != null'>AND title like CONCAT('%',#{title},'%') </if> " +
            "<if test ='content != null'>AND content like CONCAT ('%',#{content},'%') </if> " +
            "order by create_time desc" +
            "</script>"
    )
    Page<TrendsVO> pageQuery(TrendsPageQueryDTO trendsPageQueryDTO);


    /**
     * 根据id查询动态
     *
     * @param id
     * @return
     */
    @Select("select * from trends where id = #{id}")
    Trends getById(Long id);

    /**
     * 根据id删除动态
     *
     * @param id
     * @return
     */
    @Delete("delete from trends where id = #{id}")
    void deleteById(Long id);

    /**
     * 更改动态信息
     *
     * @param trends
     * @return
     */
    @Update("<script>" +
            "update trends set " +
            "<if test='title != null'> title = #{title}, </if> " +
            "<if test='image != null'> image = #{image}, </if> " +
            "<if test='content != null'> content = #{content}, </if> " +
            "<if test='updateTime != null'> update_time =#{updateTime}, </if>" +
            "<if test='updateUser != null'> update_user =#{updateUser} </if>" +
            "where id = #{id} " +
            "</script>")
    @AutoFill(value = OperationType.UPDATE)
    void update(Trends trends);

}
